<!DOCTYPE html>
<html>
	<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="../js/plugins/jrender/jrender.min.js"></script>
		<script src="../js/plugins/jquery-form/jquery.form.js"></script>

		<script>
			$(function () {
				var userStr = sessionStorage.getItem("user");
				var userJSON;
				if (userStr) {
                    userJSON = JSON.parse(userStr);
                    $(".info").renderValues(userJSON);
                    //背景墙的回显
                    $(".myBg").css("background-image","url('"+userJSON.coverImgUrl+"')");
                }

                var flag ;
                //头像编辑图标绑定点击事件
				$("#headBtn").click(function () {
                   $("#imageInput").click();
					flag = 1;
                })

                //背景编辑图标绑定点击事件
                $("#coverBtn").click(function () {
                    $("#imageInput").click();
                    flag = 0;
                })

                //绑定值改变事件
                $("#imageInput").change(function () {
                    //如果有值
                    if(this.value){
                        $("#imageForm").ajaxSubmit(function (data) {
                            if (data.status==1){
                                if (flag==1){
                                    $("#headImg").attr("src",data.url);
                                    //给表单设置头像路径
									$("#headImgUrl").val(data.url);
								}else{
                                    $(".myBg").css("background-image","url('"+data.url+"')");
									//给表单设置背景路径
                                    $("#coverImgUrl").val(data.url);
								}

                            }
                        })
                    }
                })

				//设置个人资料页面保存操作
				$("#saveBtn").click(function () {
					$("#infoForm").ajaxSubmit(function (data) {
					    if(data.success){
					        //用于编辑个人资料信息后重新更新静态页面的缓存数据
					        sessionStorage.setItem("user",JSON.stringify(data.data))
                            window.location.href = "/mine/profiles.html";
						}
                    })
                })

            })
		</script>



	</head>

	<body>

		<div class="search-head">
			<div class="row nav-search">
				<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a href="javascript:;" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>

		<div class="container info">
			<div class="myBg">
				<img id="headImg" render-src="headImgUrl"  class="rounded-circle"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<!--图片提交表单-->
			<form action="/images" method="post"  enctype="multipart/form-data" id="imageForm">
				<input type="file" name="pic" style="display: none" id="imageInput">
			</form>


			<!--编辑的表单-->
			<form id="infoForm" method="post" action="/users">
					<input type="hidden" name="id" render-value="id">
					<input type="hidden" name="_method" value="PUT">
				<!--设置隐藏域用于提交表单时一并把图片提交-->
					<input type="hidden" name="headImgUrl" id="headImgUrl" render-value="headImgUrl">
				    <input type="hidden" name="coverImgUrl" id="coverImgUrl" render-value="coverImgUrl">
				
					<label>昵称</label><input render-value="nickName" type="text" name="nickName" class="form-control" >
					<label>性别</label><select render-value="gender" class="form-control" name="gender" >
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input render-value="place" type="text" name="place"  class="form-control">
					<label>签名</label><textarea render-value="sign" name="sign" class="form-control" rows="5" ></textarea>
			</form>
		</div>
		
		
		
	</body>

</html>
